using System;
using System.Collections.Generic;
using System.Text;

using nScreen;
using nScreen.Controls;
using System.Timers;

namespace nScreen.Test
{
	public class TestConsoleScreen : Screen
	{
		private Panel _boxA;
		private Label _labelA;
		private Label _labelB;
		private HorizontalLine _horizontalLine;
		private VeridicalLine _verdicalLine;
		private ProgressBar	_progressBar;

		Timer timer = new Timer(20);
		
		protected override void OnInitialize()
		{
			base.OnInitialize();

			ScreenApplication.SuspendRender();
			ScreenApplication.KeyPress += new EventHandler<ScreenApplicationKeyPressEventArgs>(ScreenApplication_KeyPress);

			this.Title = "Test Screen";
			this.Width = 85;
			this.Height = 30;

			_boxA = new Panel();

			_boxA.Width = 16;
			_boxA.Height = 4;
			_boxA.Top = 2;
			_boxA.Left = 2;

			this.Controls.Add(_boxA);

			_labelA = new Label();

			_labelA.Top = 4;
			_labelA.Left = 4;
			_labelA.Text = "Hello World!";

			this.Controls.Add(_labelA);

			_labelB = new Label();

			_labelB.Top = 24;
			_labelB.Left = 2;
			_labelB.Text = "Press 'Q' to quit. Use the arrow keys to move the 'Hello World!' box.";
			_labelB.ForegroundColor = ConsoleColor.Yellow;

			this.Controls.Add(_labelB);

			_horizontalLine = new HorizontalLine();

			_horizontalLine.Top = 10;
			_horizontalLine.Left = 2;
			_horizontalLine.Length = 10;

			this.Controls.Add(_horizontalLine);

			_verdicalLine = new VeridicalLine();

			_verdicalLine.Top = 2;
			_verdicalLine.Left = 77;
			_verdicalLine.Length = 10;

			this.Controls.Add(_verdicalLine);

			_progressBar = new ProgressBar();
			this.Controls.Add(_progressBar);
			_progressBar.Height = 10;

			timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
			this.timer.Start();

			ScreenApplication.ResumeRender();
		}

		void timer_Elapsed(object sender, ElapsedEventArgs e)
		{
			if (_progressBar.Value == _progressBar.Maximun)
				_progressBar.Value = 0;

			_progressBar.Value++;
		}

		private void ScreenApplication_KeyPress(object sender, ScreenApplicationKeyPressEventArgs e)
		{
			switch (e.ConsoleKeyInfo.Key)
			{
				case ConsoleKey.LeftArrow:
					_labelA.Left--;
					_boxA.Left--;
					break;
				case ConsoleKey.RightArrow:
					_labelA.Left++;
					_boxA.Left++;
					break;
				case ConsoleKey.UpArrow:
					_labelA.Top--;
					_boxA.Top--;
					break;
				case ConsoleKey.DownArrow:
					_labelA.Top++;
					_boxA.Top++;
					break;
				case ConsoleKey.Q:
					ScreenApplication.Exit();
					break;
			}
		}
	}
}